package com.krt.webapp.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.krt.webapp.R;
import com.krt.webapp.until.AppInfo;
import com.krt.webapp.until.MyApplication;
import com.lxlib.myalbumlib.AlbumConstant;
import com.lxlib.myalbumlib.AlbumFragment_MA;
import com.lxlib.myalbumlib.AlbumFragment_MA.AlbumListener;

public class AlbumsActivity extends AppCompatActivity implements AlbumListener, OnClickListener {

    private TextView title;
    private AlbumFragment_MA af;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_albums);
        AppInfo appInfo = (new MyApplication(getApplication())).getAppInfo();
        sp = this.getSharedPreferences(appInfo.getSp_name(), 0);

        title = (TextView) findViewById(R.id.textView1);
        title.setOnClickListener(this);
        FragmentManager fm = getSupportFragmentManager();

        if (savedInstanceState == null) {
            af = new AlbumFragment_MA();
            fm.beginTransaction().add(R.id.f2, af, "af").commit();
        } else {
            af = (AlbumFragment_MA) fm.findFragmentByTag("af");
        }
        af.setaListener(this);
        AlbumConstant.LAST_PPATH = sp.getString("lastpath", "");
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // TODO 自动生成的方法存根
        super.onSaveInstanceState(outState);
    }

    @Override
    public void pathChange(String path) {
        // TODO 自动生成的方法存根
        sp.edit().putString("lastpath", path).commit();
    }

    @Override
    public void selectNumChange(int num) {
        // TODO 自动生成的方法存根
        title.setText("完成\n(" + num + "/" + af.PICNO + ")");
    }

    @Override
    public void onClick(View v) {
        // TODO 自动生成的方法存根
        if (v.getId() == R.id.textView1) {
            setResult(RESULT_OK, af.getBackList());
            finish();
        }
    }
}
